package com.shcz.migu.utils;

import android.content.Context;
import android.util.Log;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSStsTokenCredentialProvider;
import com.alibaba.sdk.android.oss.model.ObjectMetadata;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.shcz.migu.R;
import com.shcz.migu.resp.result.UploadResp;
import com.shcz.migu.ui.bean.AvaterUploadBean;

import java.io.File;

/**
 * Created by Sea Zhang on 2017/3/20.
 */

public class OSSClientUtil {


    /**
     * Description: 判断OSS服务文件上传时文件的contentType
     *
     * @param FilenameExtension 文件后缀
     * @return String
     */
    public static String getcontentType(String FilenameExtension) {
        if (FilenameExtension.equalsIgnoreCase("bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equalsIgnoreCase("gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equalsIgnoreCase("jpeg") ||
                FilenameExtension.equalsIgnoreCase("jpg") ||
                FilenameExtension.equalsIgnoreCase("png")) {
            return "image/jpeg";
        }
        if (FilenameExtension.equalsIgnoreCase("html")) {
            return "text/html";
        }
        if (FilenameExtension.equalsIgnoreCase("txt")) {
            return "text/plain";
        }
        if (FilenameExtension.equalsIgnoreCase("vsd")) {
            return "application/vnd.visio";
        }
        if (FilenameExtension.equalsIgnoreCase("pptx") ||
                FilenameExtension.equalsIgnoreCase("ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (FilenameExtension.equalsIgnoreCase("docx") ||
                FilenameExtension.equalsIgnoreCase("doc")) {
            return "application/msword";
        }
        if (FilenameExtension.equalsIgnoreCase("xml")) {
            return "text/xml";
        }
        if (FilenameExtension.equalsIgnoreCase("mp3")) {
            return "mp3";
        }
        return "image/jpeg";
    }

    public static String postAvater(UploadResp uploadAvater, String filePath, Context mContext) {
//        filePath="/storage/emulated/0/qqmusic/landscape/overlayVideo_mask.mp4";
        String endpoint = uploadAvater.getResult().getEndpoint();
// accessKey请登录https://ak-console.aliyun.com/#/查看
        final String accessKeyId = uploadAvater.getResult().getAccessKey();
        final String accessKeySecret = uploadAvater.getResult().getAccessSecret();
        String key = uploadAvater.getResult().getKey();
        final String token = uploadAvater.getResult().getStsToken();
        String bucketName = uploadAvater.getResult().getBucket();

// accessKey请登录https://ak-console.aliyun.com/#/查看
        OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider
                (accessKeyId, accessKeySecret, token);

// 创建OSSClient实例
        OSSClient ossClient = new OSSClient(mContext, endpoint, credentialProvider);

        //创建上传Object的Metadata
        ObjectMetadata objectMetadata = new ObjectMetadata();
        long length = new File(filePath).length();
        objectMetadata.setContentLength(length);
        objectMetadata.setCacheControl("no-cache");
        objectMetadata.setHeader("Pragma", "no-cache");
//        objectMetadata.setContentType(OSSClientUtil.getcontentType(filePath.substring(filePath.lastIndexOf("."))));
        objectMetadata.setContentDisposition("inline;filename=" + filePath);
        PutObjectRequest putObjectRequest;
        putObjectRequest = new PutObjectRequest(bucketName, key, filePath, objectMetadata);

// 创建上传Object的Metadata

// 上传文件
        try {
            PutObjectResult putResult = ossClient.putObject(putObjectRequest);
            if (putResult.getStatusCode() == 200) {
                AvaterUploadBean avaterUploadBean = new AvaterUploadBean();
                avaterUploadBean.setBucket(bucketName);
                avaterUploadBean.setKey(key);
                avaterUploadBean.setVendor(uploadAvater.getResult().getVendor());
                String result = avaterUploadBean.toString();//json字符串
//                resultStr="{\"avatar\":{'vendor':'ali','bucket':'luoqi-sandbox','key':'u/2017/3/20/58cf7835ddbe9358219fb5179PqkJMtUIV.jpg'}}";
                Log.i("avaterResult", result);
                return result;

            } else {
                CommonToast.showToast(mContext.getString(R.string.update_avater_failed));
                return "";
            }
        } catch (ClientException e) {
            CommonToast.showToast(e.getMessage() + "");
            e.printStackTrace();
        } catch (ServiceException e) {
            e.printStackTrace();
            CommonToast.showToast(e.getMessage() + "");
        }
        return "";
    }


}
